cap log close
clear all
set more off
set matsize 11000
set maxvar 120000

*Main data pathway globals, set directory to clean data folder
global results "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\ResultsRev\"
global cleandata "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\CleanData\"

cd "$cleandata"


*--------------------------------------------------------------------------
* ratings
*--------------------------------------------------------------------------
use "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\CleanData\GP_Ratings.dta" , clear
rename BehandlerId  str_exogGPIDnew
drop if str_exogGPIDnew ==0
rename OVERALL_RATING rate
ge no_miss = rate !=.
ge top = rate == 5 if rate!=.
ge bottom = rate == 1 if rate!=. 

tab1 AVAILABILITY_PHONE_QUEUE_RATING AVAILABILITY_BOOKING_TIME_RATING AVAILABILITY_WAITING_TIME_RATING TRUST_ADVICE_RATING TRUST_INSIGHT_RATING TRUST_LISTENING_RATING TRUST_TIME_USED_RATING SERVICE_OPENING_HOURS_RATING SERVICE_STAFF_RATING SERVICE_FACILITIES_RATING 

rename	 AVAILABILITY_PHONE_QUEUE_RATING 	 AV_PHONE_QUEUE 
rename	 AVAILABILITY_BOOKING_TIME_RATING 	 AV_BOOKING_TIME 
rename	 AVAILABILITY_WAITING_TIME_RATING 	 AV_WAITING_TIME 
rename	 TRUST_ADVICE_RATING 	 TRUST_ADVICE 
rename	 TRUST_INSIGHT_RATING 	 TRUST_INSIGHT 
rename	TRUST_LISTENING_RATING 	TRUST_LISTENING 
rename	 TRUST_TIME_USED_RATING 	 TRUST_TIME_USED 
rename	 SERVICE_OPENING_HOURS_RATING 	 S_OPENING_HOURS 
rename	 SERVICE_STAFF_RATING 	 S_STAFF 
rename	 SERVICE_FACILITIES_RATING 	 S_FACILITIES 


for var no_miss AV_PHONE_QUEUE AV_BOOKING_TIME AV_WAITING_TIME TRUST_ADVICE TRUST_INSIGHT TRUST_LISTENING TRUST_TIME_USED S_OPENING_HOURS S_STAFF S_FACILITIES: ge mX= X==5 if X!=.


collapse (sum) no_miss mno_miss (mean) rate top bottom AV_PHONE_QUEUE AV_BOOKING_TIME AV_WAITING_TIME TRUST_ADVICE TRUST_INSIGHT TRUST_LISTENING TRUST_TIME_USED S_OPENING_HOURS S_STAFF S_FACILITIES mAV_PHONE_QUEUE mAV_BOOKING_TIME mAV_WAITING_TIME mTRUST_ADVICE mTRUST_INSIGHT mTRUST_LISTENING mTRUST_TIME_USED mS_OPENING_HOURS mS_STAFF mS_FACILITIES , by(str_exogGPIDnew)

cd "$cleandata"
save temp_rate, replace

count
*--------------------------------------------------------------------------
* statistics (Table A6)
*--------------------------------------------------------------------------"
local rating "rate no_miss"
local top "top mno_miss"
estpost tabstat  `rating', statistics(mean) columns(statistics)
mat G0 =[e(mean)]'
mat G1 = G0
estpost tabstat  `top', statistics(mean) columns(statistics)
mat G0 =[e(mean)]'
mat G1 = [G1,G0]
*6,119

*---------------------------------------------------------*
*Table A19: Correlation between diff types of fe with ratings
*---------------------------------------------------------*
merge 1:1 str_exogGPIDnew using "$results/fe_addon2.dta"
ge no_rate = _m==2

* more is good
replace st_afe_death2_55 = -st_afe_death2_55

local rating "rate"
foreach var in st_afe_npr255 st_afe_npr_noer255 st_afe_npr_er255 st_afe_npr_in255 st_afe_npr_out255 st_afe_sl_days255  {
	eststo `var': qui reg  `var' `rating', robust  
}
	
esttab st_afe_npr255 st_afe_npr_noer255 st_afe_npr_er255 st_afe_npr_in255 st_afe_npr_out255 st_afe_sl_days255   using "$results/multi_rating.tex", booktabs legend replace label b(4) se(4) r2 star(* 0.1 ** 0.05 *** 0.01) 


*--------------------------------------------------------------------------
* analysis
*--------------------------------------------------------------------------
cd "$cleandata"
use clean_patientlevel_file_any_rev3, clear
keep if str_exogGPIDnew!=. & yr_str_exog_swap >= 2005 & yr_str_exog_swap<=2014
merge m:1 str_exogGPIDnew using "$results/fe_addon2.dta", keepusing(afe_death2_55)
drop _m

* recode main VA measure (more VA - better doc, less mortality) 
bys str_exogGPIDnew: ge n = _n
ge newva = -afe_death2_55
su newva if n == 1, d
gen stdva = (newva-r(mean))/r(sd)



* doc characteristics
ge dr_old = dr_age_str_exog_swap >=40 if dr_age_str_exog_swap!=.
ge dr_capacity = dr_max_str_exog_swap-dr_list_length_str_exog_swap >=0


foreach var in reimburse_base sl_base   {
	bys str_exogGPIDnew: egen m`var' = mean(`var')
	replace  m`var' = 0 if  m`var'==.
	}

replace dr_list_length_str_exog_swap = dr_list_length_str_exog_swap/1000
	
lab var dr_old "GP 40y.o. or older" 
lab var dr_spec_str_exog_swap "GP has specialization" 
lab var dr_list_length_str_exog_swap "GP list lenght"
lab var dr_male_str_exog_swap  "GP is male"
lab var dr_share_str_exog_swap "GP has a shared list"
lab var dr_group_str_exog_swap "GP works in shared office"
lab var  dr_max_str_exog_swap "GP max list lenght"

lab var msl_base  				"Sick Leave"
lab var mreimburse_base			"Reimbursement"

ge post_exog_swap = yr_end_swap-yr_str_exog_swap
ge anyswap = post_exog_swap>0 & yr_end_swap !=. if yr_str_exog_swap!=.

ge anyswap_gp = post_exog_swap>0 if yr_str_exog_swap!=.


global dr_exo 	"dr_old dr_spec_str_exog_swap dr_list_length_str_exog_swap  dr_male_str_exog_swap"
global type 	"msl_base mreimburse_base"


* -----------------------------------------------------------------------------
* correlates of VA: rating & other variables 
* -----------------------------------------------------------------------------
collapse (mean)  stdva $type $dr_exo anyswap anyswap_gp, by(str_exogGPIDnew)
keep if stdva!=.

merge m:1 str_exogGPIDnew   using "temp_rate"
ge no_rate = _merge == 1
drop _merge
erase temp_rate.dta

global dr_exo 	"dr_old dr_spec_str_exog_swap dr_list_length_str_exog_swap  dr_male_str_exog_swap"
global type 	"msl_base mreimburse_base"

lab var stdva "Standardized VA"
lab var dr_old "GP 40y.o. or older" 
lab var dr_spec_str_exog_swap "GP has specialization" 
lab var dr_list_length_str_exog_swap "GP list lenght"
lab var dr_male_str_exog_swap  "GP is male"
lab var msl_base  				"Sick Leave"
lab var mreimburse_base			"Reimbursement"

* Table 8
eststo e1: reg anyswap stdva, robust
su anyswap if e(sample), d
estadd scalar mean = r(mean)
eststo e1

eststo e2: reg anyswap $dr_exo $type, robust
esttab e1 e2  using "$results/endog2.tex",  booktabs legend  replace label b(5) se(5) star(* 0.1 ** 0.05 *** 0.01) stats(emp mean N, fmt(%9.3g)) 


local i = 1
foreach var in no_miss rate top bottom no_rate   {
	eststo c`i': reg  `var' stdva if stdva !=., robust  
	su `var' if e(sample)
	estadd scalar mean = r(mean)
	eststo c`i'
	local i = `i' + 1
}

foreach var in no_miss rate top bottom no_rate   {
	eststo c`i': reg  `var' $dr_exo $type if stdva !=., robust   
	local i = `i' + 1
}

foreach var in no_miss rate top bottom no_rate {
	eststo `var': reg  anyswap_gp `var' if stdva !=., robust  
}

* Table 9
esttab c1 c2 c3 c4 c5 using "$results/endog_rating.tex", booktabs legend replace label b(4) se(4) r2 star(* 0.1 ** 0.05 *** 0.01) stats(emp mean N, fmt(%9.3g)) mtitles("Nb miss rates" "Mean Rate" "Share Max" "Share Min" "No Rating") 
esttab c6 c7 c8 c9 c10 using "$results/endog_rating.tex", booktabs legend append label b(4) se(4) r2 star(* 0.1 ** 0.05 *** 0.01) stats(emp mean N, fmt(%9.3g)) 

* Table 10
esttab no_miss rate top bottom no_rate using "$results/swap_rating.tex", booktabs legend replace label b(4) se(4) r2 star(* 0.1 ** 0.05 *** 0.01) stats(emp N, fmt(%9.3g)) mtitles("Nb miss rates" "Mean Rate" "Share Max" "Share Min" "No Rating") 


su no_miss
su no_miss if stdva !=.

local rating "rate no_miss"
local top "top mno_miss"
estpost tabstat  `rating' if stdva !=., statistics(mean N) columns(statistics)
mat G0 =[e(mean)]'
mat G1 = [G1,G0]
estpost tabstat  `top'  if stdva !=., statistics(mean) columns(statistics)
mat G0 =[e(mean)]'
mat G1 = [G1,G0]

* Table A6
mat coln G1 = "Mean" "Mean" "Mean" "Mean"
mat rown G1 = "Rating" "Number of Ratings" 
count
esttab matrix(G1, fmt(3)) using "$results/summary_stats.tex", mlabel("\multicolumn{2}{c}{All} & \multicolumn{2}{c}{Sample}") replace 

